<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面试宝典后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <template #header>
        <div class="header">
          <span>共 {{ total }} 条记录</span>
          <el-button
            @click="openDrawer(1)"
            icon="el-icon-plus"
            size="small"
            type="primary"
            round
          >
            添加面经
          </el-button>
        </div>
      </template>
      <el-table stripe :data="list" style="width: 100%">
        <el-table-column prop="stem" label="标题" width="400">
        </el-table-column>
        <el-table-column prop="creator" label="作者"> </el-table-column>
        <el-table-column prop="likeCount" label="点赞"> </el-table-column>
        <el-table-column prop="views" label="浏览"> </el-table-column>
        <el-table-column prop="createdAt" label="更新日期" width="180">
        </el-table-column>
        <el-table-column label="操作" width="120">
          <template #default="{ row }"
            ><div class="actions">
              <i class="el-icon-view" @click="openDrawer(2, row.id)"></i>
              <i class="el-icon-edit" @click="openDrawer(3, row.id)"></i>
              <i class="el-icon-delete" @click="del(row.id)"></i></div
          ></template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-sizes="[10, 20, 25, 50]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <el-drawer
      :title="drawTitle"
      :visible.sync="drawer"
      :before-close="handleClose"
      size="50%"
    >
      <!-- 添加修改抽屉里是表单 -->
      <el-form
        v-if="drawOpt !== 2"
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="标题" prop="stem">
          <el-input v-model="form.stem"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <quill-editor
            v-model="form.content"
            @blur="$refs.form.validateField('content')"
          ></quill-editor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit('form')">确认</el-button>
          <el-button @click="handleClose">取消</el-button>
        </el-form-item>
      </el-form>
      <!-- 查看时抽屉是div -->
      <div v-else class="article-preview">
        <h5>{{ form.stem }}</h5>
        <div v-html="form.content"></div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import {
  getArticleApi,
  createArticleApi,
  deleteArticleApi,
  getSingleArticleApi,
  updateSingleArticleApi
} from '@/api/article'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'
export default {
  name: 'article-page',
  components: {
    quillEditor
  },
  created () {
    this.initData()
  },
  methods: {
    async initData () {
      const res = await getArticleApi({
        current: this.current,
        pageSize: this.pageSize
      })
      this.list = res.data.rows
      this.total = res.data.total
    },
    async del (id) {
      await deleteArticleApi({ id: id })
      this.$message.success('删除成功')
      this.initData()
    },
    handleSizeChange (pageSize) {
      this.pageSize = pageSize
      this.initData()
    },
    handleCurrentChange (current) {
      this.current = current
      this.initData()
    },
    handleClose () {
      // delete this.form.id
      // this.$refs.form.resetFields()
      this.form = {
        stem: '',
        content: ''
      }
      this.drawer = false
    },
    async openDrawer (opt, id) {
      this.drawOpt = opt
      if (this.drawOpt !== 1) {
        const res = await getSingleArticleApi(id)
        this.form.stem = res.data.stem
        this.form.content = res.data.content
        this.form.id = res.data.id
      }
      this.drawer = true
    },

    onSubmit (formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          if (this.drawOpt === 1) {
            try {
              await createArticleApi(this.form)
              this.$message.success('添加成功')
              this.drawer = false
              this.form.content = this.form.stem = ''
              this.initData()
            } catch (err) {
              if (err.response) {
                this.$message.error(err.response.data.message)
              } else {
                this.$message.error('添加失败')
              }
            }
          } else if (this.drawOpt === 3) {
            try {
              await updateSingleArticleApi(this.form)
              this.$message.success('修改成功')
              this.drawer = false
              this.form.content = this.form.stem = ''
              this.initData()
            } catch (err) {
              if (err.response) {
                this.$message.error(err.response.data.message)
              } else {
                this.$message.error('修改失败')
              }
            }
          }
        }
      })
    }
  },
  computed: {
    drawTitle () {
      if (this.drawOpt === 1) {
        return '添加面经'
      } else if (this.drawOpt === 2) {
        return '查看面经'
      } else {
        return '编辑面经'
      }
    }
  },
  data () {
    return {
      list: [],
      total: 0,
      current: 1,
      pageSize: 10,
      drawer: false,
      drawOpt: 1,
      form: {
        stem: '',
        content: ''
      },
      rules: {
        stem: [
          {
            required: true,
            message: '请输入标题',
            trigger: 'blur'
          }
        ],
        content: [
          {
            required: true,
            message: '请输入内容',
            trigger: 'blur'
          }
        ]
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
